package com.lzw.breezeNovel.WebAdmin.service.impl;


import com.lzw.breezeNovel.WebAdmin.mapper.CategroyMapper;
import com.lzw.breezeNovel.WebAdmin.service.CategroyService;
import com.lzw.breezeNovel.WebAdmin.vo.CategoryVo;
import com.lzw.breezeNovel.model.entity.pojo.Category;
import com.lzw.breezeNovel.model.entity.query.PaginationResultQuery;
import com.lzw.breezeNovel.model.entity.query.SimplePage;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@SuppressWarnings("all")
@Service
public class CategroyServiceImpl implements CategroyService {
    /**
     * 注入mapper
     */
    @Resource
    private CategroyMapper categroyMapper;

    // 使用ThreadLocal确保日期格式化的线程安全
    private static final ThreadLocal<DateFormat> DATE_FORMATTER = ThreadLocal.withInitial(
            () -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    );


    /**
     * 添加分类
     * @param categroy
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCategroys(Category categroy) {
        //新增分类
        categroyMapper.insert(categroy);
    }

    /**
     * 获取所有的分类
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public PaginationResultQuery<CategoryVo> getAllCategory(Integer page, Integer pageSize) {
        //对分页条件进行判断
        page =  Math.max(1, Optional.ofNullable(page).orElse(1));
        pageSize = Math.max(1, Optional.ofNullable(pageSize).orElse(10));
        //获取所有的分类
        Integer count = categroyMapper.selectCount();
        if(count == 0){
            return new PaginationResultQuery<>(count, pageSize, page, new ArrayList<>());
        }
        //创建分页对象
        SimplePage simplePage = new SimplePage(page, count, pageSize);
        //进行分页计算
        simplePage.action();
        List<Category> categoryList = categroyMapper.selectAll(simplePage);
        //循环遍历
        List<CategoryVo> categoryVoList = new ArrayList<>();
        //判断是否存在
        if(categoryList != null && categoryList.size() > 0){
            for (var category : categoryList) {
                //创建CategoryVo对象
                CategoryVo categoryVo = new CategoryVo();
                BeanUtils.copyProperties(category, categoryVo);
                //获取创建时间
                categoryVo.setCreateTime(DATE_FORMATTER.get().format(category.getCreateTime()));
                //获取类型
                String type = category.getType() == 1 ? "女" : "男";
                categoryVo.setType(type);
                //获取书籍数量
                Long bookCount = category.getBookCount();
                categoryVo.setBookCount(bookCount.intValue());
                categoryVoList.add(categoryVo);
            }
        }
        return new PaginationResultQuery<>(count, pageSize, page, categoryVoList);
    }

    /**
     * 修改分类
     * @param categroy
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCategory(Category categroy) {
        categroyMapper.update(categroy);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteCategoryById(Integer id) {
        categroyMapper.deleteCategoryById(id);
    }

    /**
     *  批量删除
     * @param ids
     */
    @Override
    public void deleteCategoryByIds(Integer[] ids) {
        categroyMapper.deleteCategoryByIds(ids);
    }

    /**
     * 查询所有分类
     * @return
     */
    @Override
    public List<CategoryVo> getAllCategorys() {
        //创建集合
        List<CategoryVo> categoryVos = new ArrayList<>();
        //查询所有分类
        List<Category> categoryList = categroyMapper.select();
        //判断是否存在
        if (categoryList != null && categoryList.size() > 0) {
            for (var category : categoryList) {
                CategoryVo categoryVo = new CategoryVo();
                BeanUtils.copyProperties(category,categoryVo);
                //添加到集合里面
                categoryVos.add(categoryVo);
            }
        }
        return categoryVos;
    }


}
